package com.pay.clickevent


import android.accounts.Account
import okhttp3.*
import org.json.JSONObject
import java.lang.Exception

class HttpUtil {

    companion object {
        //心跳频率
        var heartBeat = 0
        val client = OkHttpClient.Builder().build() //初始化请求
        var url = "http://smartpays.online:7000/Pay/"


        fun PostBill(account: String, money: String, date: String, time: String): JSONObject? {
            try {
                val params = FormBody.Builder()
                        .add("Account", account)
                        .add("ACTUAL_AMOUNT", money)
                        .add("Date", date)
                        .add("Time", time)
                        .build()//请求表单
                var request = Request.Builder().url(url + "trans/ZZ2AliNotify").post(params).build()//构建请求

                var response = client.newCall(request).execute()
                var result = response.body()!!.string()
                return JSONObject(result)
            } catch (e: Exception) {

                return null

            }


        }

        fun heartBeat(account: String): JSONObject? {
            try {
                var jsonObject: JSONObject? = null
                if (null != account && !"".equals(account)) {
                    val params = FormBody.Builder()
                            .add("ACCOUNT_NUMBER", account)
                            .build()//请求表单
                    var request = Request.Builder().url(url + "QRCode/heartbeat").post(params).build()//构建请求

                    var response = client.newCall(request).execute()
                    var result = response.body()!!.string()
                    println(result)
                    jsonObject = JSONObject(result)

                }

                return jsonObject


            } catch (e: Exception) {
                e.printStackTrace()
                println("服务器请求失败")
                return null
            }
        }
    }
}